<?php
class News extends CI_Model {
	
	var $Title = "";
	var $Author = "";
	var $Detail = "";
	var $Type = "";
	var $ImgDir = "";
	var $NewsDate = '';
	var $table = 'webpage_news';

    function __construct()
    {
        parent::__construct();
		$this->table = 'webpage_news';
    }
	    
    function get_last_n_entries($type, $num=5)
    {
        $query = $this->db->order_by('NewsId', 'desc');
		$query = $query->get_where($this->table, array('Type' => $type), $num);
        return $query->result();
    }
	
	function get_entries($limit, $offset, $type)
    {
        $query = $this->db->order_by('NewsId', 'desc');
		$query = $query->get_where($this->table, array('Type' => $type), $limit, $offset);
        return $query->result();
    }
	
	function get_entrie($id)
    {
        $query = $this->db->where('NewsId', $id);
		$query = $query->get($this->table);
        return $query->result();
    }
	
	function get_total_num($type)
	{
		$query = $this->db->where('Type', $type);
		return $this->db->count_all_results($this->table);
	}


    function insert_entry()
    {
        $this->Title   = $_POST['title']; // 请阅读下方的备注
        $this->Detail = $_POST['detail'];
		$this->Author = $_POST['author'];
        $this->NewsDate = date('Y-m-d');
		$this->Type = $_POST['type'];
		$this->ImgDir = $_POST['imgdir'];		
        $this->db->insert($this->table, $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }
	
	function get_link($obj)
	{
		$ret = '<a href="' . site_url('/news/item');
		$ret .= '/' . $obj->NewsId;
		$ret .= '">' . $obj->Title;
		$ret .= '</a>';
		
		return $ret;
	}
	
	function get_all_atts($id, $type)
	{
		$news_type = 0;
		if ($type == '中心动态' || $type == '学术专题' || $type == '公告通知' || $type == '精品课程')
			$news_type = 1;
		elseif ($type == '本科生教学' || $type == '研究生教学')
			$news_type = 2;
		elseif ($type == '硕士招生' || $type == '硕士生复试')
			$news_type = 3;
		
		$query = $this->db->get_where('webpage_appendix', array('NewsID' => $id, 'NewsType' => $news_type));
		
        return $query->result();
	}

}